package com.ksl.kinglockguard.utils;

import java.lang.reflect.Field;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Base64;

public class NetUtil {
	public static final String OPERATOR_MOBILE = "移动";
	public static final String OPERATOR_UNICOM = "联通";
	public static final String OPERATOR_TELCOM = "电信";
	public static final String OPERATOR_UNKOWN = "未知";

	public static final String NETWORK_2G = "2G";
	public static final String NETWORK_3G = "3G";
	public static final String NETWORK_4G = "4G";
	public static final String NETWORK_WIFI = "WIFI";
	public static final String NETWORK_UNKOWN = "UNKOWN";

	/** 打开应用的时间 **/
	public static long OPENAPPTIME = 0;

	public static final long MIN = 60 * 1000;
	public static long UPDATERULETIME = 30 * MIN;
	public static String SOUND;
	public static String POSTDATA;
	public static String vcode;
	public static String channel;
	public static String imie;
	public static String mobile;

	/**
	 * 利用反射获取Drawable的Id
	 * 
	 * @param context
	 * @param resoureId
	 *            要获取的布局的图片名称
	 * @return
	 */
	public static int getResourseDrawableId(String pkgName, String resoureId) {
		Class resClass = null;
		int resId = 0;
		Field field;
		try {
			resClass = Class.forName(pkgName + ".R$drawable");
			if (resClass != null) {
				field = resClass.getDeclaredField(resoureId);
				resId = field.getInt(resClass);
			}
			return resId;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resId;
	}

	/**
	 * 利用反射获取View的Id
	 * 
	 * @param context
	 * @param resoureId
	 *            要获取的布局的view的ID名称
	 * @return
	 */
	public static int getResourseViewId(String pkgName, String resoureId) {
		Class resClass = null;
		int resId = 0;
		Field field;
		try {
			resClass = Class.forName(pkgName + ".R$id");
			if (resClass != null) {
				field = resClass.getDeclaredField(resoureId);
				resId = field.getInt(resClass);
			}
			return resId;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resId;
	}

	/**
	 * 利用反射获取Layout的Id
	 * 
	 * @param context
	 * @param resoureId
	 *            要获取的布局的xml名称
	 * @return
	 */
	public static int getResourseLayoutId(String pkgName, String resoureId) {
		Class resClass = null;
		int resId = 0;
		Field field;
		try {
			resClass = Class.forName(pkgName + ".R$layout");
			if (resClass != null) {
				field = resClass.getDeclaredField(resoureId);
				resId = field.getInt(resClass);
			}
			return resId;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resId;
	}

	/**
	 * Base64编码字符串
	 * 
	 * @param key
	 * @return 经过编码后的字符串
	 */
	public static String Base64Encode(byte[] bytes) {
		return bytes == null ? null : new String(Base64.encode(bytes,
				Base64.DEFAULT));
	}

	/**
	 * Base64解码
	 * 
	 * @param bytes
	 *            需要解码的字节数组
	 * @return 解码后的字符串
	 */
	public static String Base64Decode(byte[] bytes) {
		return bytes == null ? null : new String(Base64.decode(bytes,
				Base64.DEFAULT));
	}

	public static String getMeta(Context context, String key) {
		ApplicationInfo appInfo;
		String msg = "";
		try {
			appInfo = context.getPackageManager().getApplicationInfo(
					context.getPackageName(), PackageManager.GET_META_DATA);
			msg = appInfo.metaData.getString(key);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		return msg;
	}

	/**
	 * 获取运营商名称
	 * 
	 * @param context
	 * @return 联通 移动 电信 未知
	 */
	public static String getOperetorName(Context context) {
		TelephonyManager mTelManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String operator = mTelManager.getSimOperator();
		if (operator.equals("46000") || operator.equals("46002")) {
			return OPERATOR_MOBILE;
		} else if (operator.equals("46001")) {
			return OPERATOR_UNICOM;
		} else if (operator.equals("46003")) {
			return OPERATOR_TELCOM;
		}
		return OPERATOR_UNKOWN;
	}

	public static String getFirstStr() {
		return "~@RT%$TGSF244)-+))78#DF7*(%^";
	}

	/**
	 * 获取设备sim卡的网络类型，,不包括wifi
	 * 
	 * @param context
	 * @return 2G 3G 4G UNKOWN
	 */
	public static String getMobileNetType(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		int type = tm.getNetworkType();
		switch (type) {
		case TelephonyManager.NETWORK_TYPE_GPRS:
		case TelephonyManager.NETWORK_TYPE_EDGE:
		case TelephonyManager.NETWORK_TYPE_CDMA:
		case TelephonyManager.NETWORK_TYPE_1xRTT:
		case TelephonyManager.NETWORK_TYPE_IDEN:
			return NETWORK_2G;
		case TelephonyManager.NETWORK_TYPE_UMTS:
		case TelephonyManager.NETWORK_TYPE_EVDO_0:
		case TelephonyManager.NETWORK_TYPE_EVDO_A:
		case TelephonyManager.NETWORK_TYPE_HSDPA:
		case TelephonyManager.NETWORK_TYPE_HSUPA:
		case TelephonyManager.NETWORK_TYPE_HSPA:
		case TelephonyManager.NETWORK_TYPE_EVDO_B:
		case TelephonyManager.NETWORK_TYPE_EHRPD:
		case TelephonyManager.NETWORK_TYPE_HSPAP:
			return NETWORK_3G;
		case TelephonyManager.NETWORK_TYPE_LTE:
			return NETWORK_4G;
		default:
			return NETWORK_UNKOWN;
		}
	}

	/**
	 * 包括wifi网络
	 * 
	 * @param context
	 * @return
	 */
	public static String getNetType(Context context) {
		ConnectivityManager mConnManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = mConnManager.getActiveNetworkInfo();
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		int type = tm.getNetworkType();
		if (info != null) {
			if (info.getType() == ConnectivityManager.TYPE_WIFI) {
				return NETWORK_WIFI;
			} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
				int subtype = info.getSubtype();
				switch (subtype) {
				case TelephonyManager.NETWORK_TYPE_GPRS:
				case TelephonyManager.NETWORK_TYPE_EDGE:
				case TelephonyManager.NETWORK_TYPE_CDMA:
				case TelephonyManager.NETWORK_TYPE_1xRTT:
				case TelephonyManager.NETWORK_TYPE_IDEN:
					return NETWORK_2G;
				case TelephonyManager.NETWORK_TYPE_UMTS:
				case TelephonyManager.NETWORK_TYPE_EVDO_0:
				case TelephonyManager.NETWORK_TYPE_EVDO_A:
				case TelephonyManager.NETWORK_TYPE_HSDPA:
				case TelephonyManager.NETWORK_TYPE_HSUPA:
				case TelephonyManager.NETWORK_TYPE_HSPA:
				case TelephonyManager.NETWORK_TYPE_EVDO_B:
				case TelephonyManager.NETWORK_TYPE_EHRPD:
				case TelephonyManager.NETWORK_TYPE_HSPAP:
					return NETWORK_3G;
				case TelephonyManager.NETWORK_TYPE_LTE:
					return NETWORK_4G;
				default:
					return NETWORK_UNKOWN;
				}
			}
		}
		return NETWORK_UNKOWN;
	}
}
